//
//  SecondViewController.swift
//  JuSwift
//
//  Created by Juvid on 2018/5/10.
//  Copyright © 2018年 Juvid. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    var ju_collect : UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        JuGeneric().start()
        let layout = UICollectionViewFlowLayout.init()
        ju_collect = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
        ju_collect.backgroundColor = UIColor.white
        ju_collect.delegate=self;
        ju_collect.dataSource = self
        ju_collect.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        self.view.addSubview(ju_collect)

        // Do any additional setup after loading the view.
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 30
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        var lab : UILabel? = cell.viewWithTag(111) as? UILabel
        if lab == nil {
            lab = UILabel.init(frame: CGRect.init(x: 10, y: 0, width: 200, height: 45))
            lab?.tag=111;
            cell.addSubview(lab!)
        }
        lab?.text = "这是个collectviewcell"
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: self.view.frame.width, height: 45)
    }

    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {
            cell?.backgroundColor = UIColor.white
        }
       
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = UIColor.lightGray
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
